using System;
using System.Linq;
using NHibernate;
using NHibernate.Linq;
using Sample.Domain;

namespace Sample.Service.Queries
{
    /// <summary>
    /// Get the top 5 reviewers for all time or within a time period
    /// </summary>
    /// <remarks>http://ayende.com/Blog/archive/2009/04/18/the-dal-should-go-all-the-way-to-ui.aspx
    /// suggests this approach because the <see cref="IQueryable{T}"/> interface lets
    /// consumers of the query modify the results prior to execution
    /// through the whole delayed execution feature of Linq
    /// <code>
    /// ISession session = GetSessionFromSomewhere();
    /// var query = new TopReviewersQuery();
    /// var lazyList = query.GetQuery(session)
    /// 
    /// // Now you can either use the lazyList which will execute the query
    /// // or add more stuff on first.
    /// 
    /// lazyList.Take(2) // just get the first two results
    /// 
    /// // And now execute the query on first usage of the results
    /// foreach(var person in lazyList)
    /// {
    ///   Console.WriteLine(person.FirstName);
    /// }</code></remarks>
    public class TopReviewersQuery
    {
        public TopReviewersQuery()
        {
            Number = 5;
        }

        public DateTime? After { get; set; }
        public DateTime? Before { get; set; }

        /// <summary>
        /// The number of reviewers to get, like 
        /// top 5 or top 10 or top 1. 5 is the default
        /// </summary>
        public int Number { get; set; }

        public IQueryable<Person> GetQuery(ISession session)
        {
            IQueryable<Person> people = from person in session.Linq<Person>()
                                        select person;
            return people;
        }
    }
}